import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { ApiService } from './api.service';
import { Record} from '../record';
import {People} from '../people';
import {el} from '@angular/platform-browser/testing/src/browser_util';

@Injectable({
  providedIn: 'root'
})
export class RecordsService {
  private api_url ;
  private headers ;

  constructor(private http: Http, private apiService: ApiService) {
    this.api_url = apiService.getUrl() + '/records';
    this.headers = apiService.getHeaders();
  }
  getRecords(): Promise<Record[]> {
    const url = `${this.api_url}`;
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Record[])
      .catch(this.handleError);
  }
  getRecordsByConstraint(record: Record): Promise<Record[]> {
    let url = `${this.api_url}?`;
    console.log(record);
    if (record.bid !== undefined) {
      if (record.pid !== undefined) {
        if (record.bdate !== undefined) {
          if (record.rdate !== undefined) {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?bid=${record.bid}&pid=${record.pid}
              &bdate=${record.bdate}&rdate=${record.rdate}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?bid=${record.bid}&pid=${record.pid}&bdate=${record.bdate}&rdate=${record.rdate}`;
            }
          } else {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?bid=${record.bid}&pid=${record.pid}&bdate=${record.bdate}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?bid=${record.bid}&pid=${record.pid}&bdate=${record.bdate}`;
            }
          }
        } else {
          if (record.rdate !== undefined) {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?bid=${record.bid}&pid=${record.pid}&rdate=${record.rdate}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?bid=${record.bid}&pid=${record.pid}&rdate=${record.rdate}`;
            }
          } else {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?bid=${record.bid}&pid=${record.pid}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?bid=${record.bid}&pid=${record.pid}`;
            }
          }
        }
      } else {
        if (record.bdate !== undefined) {
          if (record.rdate !== undefined) {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?bid=${record.bid}&bdate=${record.bdate}&rdate=${record.rdate}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?bid=${record.bid}&bdate=${record.bdate}&rdate=${record.rdate}`;
            }
          } else {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?bid=${record.bid}&bdate=${record.bdate}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?bid=${record.bid}&bdate=${record.bdate}`;
            }
          }
        } else {
          if (record.rdate !== undefined) {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?bid=${record.bid}&rdate=${record.rdate}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?bid=${record.bid}&rdate=${record.rdate}`;
            }
          } else {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?bid=${record.bid}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?bid=${record.bid}`;
              console.log(url);
            }
          }
        }
      }
    } else {
      if (record.pid !== undefined) {
        if (record.bdate !== undefined) {
          if (record.rdate !== undefined) {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?pid=${record.pid}&bdate=${record.bdate}&rdate=${record.rdate}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?pid=${record.pid}&bdate=${record.bdate}&rdate=${record.rdate}`;
            }
          } else {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?pid=${record.pid}&bdate=${record.bdate}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?pid=${record.pid}&bdate=${record.bdate}`;
            }
          }
        } else {
          if (record.rdate !== undefined) {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?pid=${record.pid}&rdate=${record.rdate}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?pid=${record.pid}&rdate=${record.rdate}`;
            }
          } else {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?pid=${record.pid}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?pid=${record.pid}`;
            }
          }
        }
      } else {
        if (record.bdate !== undefined) {
          if (record.rdate !== undefined) {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?bdate=${record.bdate}&rdate=${record.rdate}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?bdate=${record.bdate}&rdate=${record.rdate}`;
            }
          } else {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?bdate=${record.bdate}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?bdate=${record.bdate}`;
            }
          }
        } else {
          if (record.rdate !== undefined) {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?rdate=${record.rdate}&realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}?rdate=${record.rdate}`;
            }
          } else {
            if (record.realDate !== undefined) {
              url = `${this.api_url}?realDate=${record.realDate}`;
            } else {
              url = `${this.api_url}`;
              console.log(url);
            }
          }
        }
      }
    }
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Record[])
      .catch(this.handleError);
  }
  getRecordsByBid(bid: string): Promise<Record[]> {
    const url = `${this.api_url}?bid=${bid}`;
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Record[])
      .catch(this.handleError);
  }
  getRecordsByPid(pid: number): Promise<Record[]> {
    const url = `${this.api_url}?pid=${pid}`;
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Record[])
      .catch(this.handleError);
  }
  getRecordsOfNotReturn(bid: string, pid: number): Promise<Record[]> {
    const url = `${this.api_url}?bid=${bid}&pid=${pid}&realDate=未还`;
    return this.http.get(url, {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Record[])
      .catch(this.handleError);
  }
  createRecord(record: Record): Promise<Record> { // 传入的为Record对象
    const url = `${this.api_url}`;
    return this.http
      .post(url, JSON.stringify(record), {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Record)
      .catch(this.handleError);
  }
  createRecordByAll(bid: string, pid: number,bdate: string,rdate: string,realDate: string): Promise<Record> {
    const record = {
      bid: bid,
      pid: pid,
      bdate: bdate,
      rdate: rdate,
      realDate: realDate
    }
    const url = `${this.api_url}`;
    return this.http
      .post(url, JSON.stringify(record), {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Record)
      .catch(this.handleError);
  }
  updateRealDate(record: Record): Promise<Record> {
    const url = `${this.api_url}/${record.id}`;
    return this.http
      .put(url, JSON.stringify(record), {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Record)
      .catch(this.handleError);
  }
  updateRDate(record: Record): Promise<Record> {
    const url = `${this.api_url}/${record.id}`;
    return this.http
      .put(url, JSON.stringify(record), {headers: this.headers})
      .toPromise()
      .then(res => res.json() as Record)
      .catch(this.handleError);
  }
  private handleError(error: any): Promise<any> {
    console.error('An error occurred', error);
    return Promise.reject(error.message || error);
  }

}
